import { orderlist } from "../../../../js/modules/order.js";
import { util,api } from '../../../../js/util';
import authorize from '../../../../js/modules/authorize.js'
import BasePage from '../../../../js/common/base-page'
const app =getApp();
BasePage({
  pageKey:"arrears",

  /**
   * 页面的初始数据
   */
  data: {
    integral: 0,
    page: {
      PageIndex: 1,
      PageSize: 20,
      Keyword: "",
      sortField: "AddTime",
      sortDirect: "desc"
    },
    list: [],
    total:0.0,
    hasMoreData: true,
    Currency:'',//货币符号
    mainColor: '', // 主题色
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    this.setData({
      mainColor: api.titleColor || "#e56b14"
    })
  },

  /**加载商品列表 */
  loadList() {
    var that = this;
    var page = this.data.page;
    orderlist.load_arrears_list(page, function (content) {
      var hasMoreData = content.count > (page.PageIndex) * page.PageSize;
      var index = that.data.list.length;
      var data = content.data;
      //当分页大于1时，累加
      if (page.PageIndex > 1) {
        data = that.data.list.concat(data);
      }
      //如果key为变量作为key值的时候要用[]包裹起来
      that.setData({
        list: data,
        hasMoreData: hasMoreData,
        isLoadComplete:true
      });
      // console.log(that.data.list);
    })
  },
  /**
* 页面相关事件处理函数--监听用户下拉动作
*/
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.data.page.PageIndex = 1;
    this.loadList();
    // console.log("onPullDownRefresh");
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    // console.log("onReachBottom");
    if (this.data.hasMoreData) {
      var index = this.data.page.PageIndex;
      this.data.page.PageIndex = index + 1;
      this.loadList();
    }
  },
  /**
 * 关键词搜索
 */
  onSearchKeywordEvent: function (e) {
    this.data.page.Keyword = e.detail;
    this.data.page.PageIndex=1;
    this.loadList();
  },
  /**
   * 计算支付金额
   */
  caculate:function(){
    var total=0;
    for(var i=0;i<this.data.list.length;i++){
      var item = this.data.list[i];
      if (!item.isChecked) continue;
      total += item.Payables;
    }
    this.setData({
      total: total,
      list: this.data.list
    });
  },
  /**
    * 单个选择事件
    */
  onSelectSingleEvent: function (e) {
    var index = e.currentTarget.dataset.index;
    this.data.list[index].isChecked = !this.data.list[index].isChecked;
    this.caculate();
  },
  /**
   * 全选事件
   */
  onSelectAllEvent: function (e) {
    this.data.select_all_status = (this.data.select_all_status == 1 ? 0 : 1);
    for (var i = 0; i < this.data.list.length; i++) {
      this.data.list[i].isChecked = (this.data.select_all_status == 1);
    }
    this.caculate();
  },
  /**
 * 货到付款，已经完成，但未付款的
 */
  repay_order: function (e) {
    var arr=[];
    for (var i = 0; i < this.data.list.length; i++) {
      var item = this.data.list[i];
      if (!item.isChecked) continue;
      arr.push(item.Id);
    }
    orderlist.repay_order(arr.join(","));
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.page.PageIndex = 1;
    this.loadList();
    this.setData({
      Currency:api.Currency || '￥'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onUnload: function (e) {
    this.data.pre_page = this.getPreData();
    this.data.pre_page.data.isBack = true;
  },
  getPreData: function () {
    const pages = getCurrentPages();
    const length = pages.length;
    const prePage = pages[length - 2]; //上一页
    return prePage;
  }
})